package work.vcloud.baseinfo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;


@RestController
public class MenuController extends BaseController{
	
	@Autowired
    OpenDao openDao;
	
	@RequestMapping("api/role_power.insert")
	public int student() throws Exception {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			JSONArray menuvalues = JSONArray.parseArray(map.get("menuvalues").toString());
			JSONArray buttonvalues = JSONArray.parseArray(map.get("buttonvalues").toString());
			JSONArray buttonmenus = JSONArray.parseArray(map.get("buttonmenus").toString());
			for(int i = 0;i<menuvalues.size();i++) {
				map.put("pk_menu", menuvalues.get(i));
				map.put("type", '2');
				map.put("pk_button", null);
				map.put("sqlMapId", "role_power.insert");
				openDao.insert(map);
			}
			for(int j = 0;j<buttonvalues.size();j++) {
				map.put("pk_menu", buttonmenus.get(j));
				map.put("pk_button", buttonvalues.get(j));
				map.put("type", '3');
				map.put("sqlMapId", "role_power.insert");
				openDao.insert(map);
			}
			return (1);
		} catch (Exception e) {
			e.printStackTrace();
			return (0);
		}
	}
	
	//课表设置-按照班级
	@RequestMapping("api/course_table.updateByClass")
	public OutputVo updateByClass() throws Exception {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			JSONArray tables = JSONArray.parseArray(map.get("course_table").toString());
			for(int i = 0;i<tables.size();i++) {
				Map<String, Object> table =(Map<String, Object>) tables.get(i);
				table.put("userInfo",map.get("userInfo"));
				if(table.get("fk_course_info")!=null){
					/*Map<String, Object> dataMap1 = new HashMap<String, Object>();
					dataMap1.put("status", "0");
					dataMap1.put("academic", map.get("academic"));
					dataMap1.put("semester", map.get("semester"));
					dataMap1.put("fk_unit", map.get("fk_unit"));
					dataMap1.put("fk_user", table.get("fk_user"));
					dataMap1.put("sqlMapId", "course_table.select");
					List<Map<String, Object>> result = openDao.queryForList(dataMap1);
					if (result.size() == 0) {
						dataMap1.put("fk_unit", null);
						dataMap1.put("week", map.get("week"));
						dataMap1.put("day", table.get("day"));
						dataMap1.put("fk_course_time", table.get("fk_course_time"));
						dataMap1.put("noclass", map.get("fk_class"));
						dataMap1.put("sqlMapId", "course_table.select");
						result = openDao.queryForList(dataMap1);
						if (result.size() == 0) {*/
							Map<String, Object> dataMap2 = new HashMap<String, Object>();
							dataMap2.put("status","0");
							dataMap2.put("academic", map.get("academic"));
							dataMap2.put("semester", map.get("semester"));
							dataMap2.put("day", table.get("day"));
							dataMap2.put("fk_class", map.get("fk_class"));
							dataMap2.put("week", map.get("week"));
							dataMap2.put("fk_course_time", table.get("fk_course_time"));
							dataMap2.put("sqlMapId", "course_table.select");
							Map<String, Object> result1 = (Map<String, Object>) openDao.queryForObject(dataMap2);
							if (result1==null) {
								table.put("fk_class",map.get("fk_class"));
								table.put("week",map.get("week"));
								table.put("sqlMapId", "course_table.insert");
								openDao.insert(table);
							}else {
								table.put("status", "99");
								table.put("id", result1.get("id"));
								table.put("sqlMapId", "course_table.update");
								openDao.update(table);
								
								table.put("fk_class",map.get("fk_class"));
								table.put("week",map.get("week"));
								table.put("sqlMapId", "course_table.insert");
								openDao.insert(table);
							}
					/*	}else {
							return output("9999",result.get(0).get("username")+"老师在星期"+result.get(0).get("day")+"的"+result.get(0).get("coursetime")+"在其他班级已有课程，一个教师不可同时上两个及两个以上班级的课！","");
						}
					}else {
						return output("9999",result.get(0).get("username")+"老师在"+result.get(0).get("unitname")+"校区已有课程，一个教师不可同时上两个及两个以上校区的课！","");
					}*/
				}else{
					map.put("day", table.get("day"));
					map.put("fk_course_time", table.get("fk_course_time"));
					map.put("sqlMapId", "course_table.delete");
					openDao.delete(map);
				}
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	//课表设置-按照教师
	@RequestMapping("api/course_table.updateByUser")
	public OutputVo updateByUser() throws Exception {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			JSONArray tables = JSONArray.parseArray(map.get("course_table").toString());
			for(int i = 0;i<tables.size();i++) {
				Map<String, Object> table =(Map<String, Object>) tables.get(i);
				table.put("userInfo",map.get("userInfo"));
				if(table.get("fk_course_info")!=null){
					/*Map<String, Object> dataMap1 = new HashMap<String, Object>();
					dataMap1.put("status", "0");
					dataMap1.put("academic", map.get("academic"));
					dataMap1.put("semester", map.get("semester"));
					dataMap1.put("fk_unit", map.get("fk_unit"));
					dataMap1.put("fk_user", map.get("fk_user"));
					dataMap1.put("sqlMapId", "course_table.select");
					List<Map<String, Object>> result = openDao.queryForList(dataMap1);
					if (result.size() == 0) {
						dataMap1.put("week", map.get("week"));
						dataMap1.put("day", table.get("day"));
						dataMap1.put("fk_course_time", table.get("fk_course_time"));
						dataMap1.put("nouser", map.get("fk_user"));
						dataMap1.put("sqlMapId", "course_table.select");
						result = openDao.queryForList(dataMap1);
						if (result.size() == 0) {*/
							Map<String, Object> dataMap2 = new HashMap<String, Object>();
							dataMap2.put("academic", map.get("academic"));
							dataMap2.put("semester", map.get("semester"));
							dataMap2.put("day", table.get("day"));
							dataMap2.put("week", map.get("week"));
							dataMap2.put("fk_course_time", table.get("fk_course_time"));
							dataMap2.put("sqlMapId", "course_table.select");
							List<Map<String, Object>> result1 = openDao.queryForList(dataMap2);
							if (result1.size() == 0) {
								table.put("week",map.get("week"));
								table.put("sqlMapId", "course_table.insert");
								openDao.insert(table);
							} else {
								for(int j = 0;j<result1.size();j++) {
									Map<String, Object> resultMap = (Map<String, Object>) result1.get(j);
									if (resultMap.get("fk_course_info").equals(table.get("fk_course_info"))) {
										if(resultMap.get("status").equals("99")) {
											table.put("status", "0");
											table.put("id", resultMap.get("id"));
											table.put("sqlMapId", "course_table.update");
											openDao.update(table);
										}
									}else {
										table.put("status", "99");
										table.put("id", resultMap.get("id"));
										table.put("sqlMapId", "course_table.update");
										openDao.update(table);
										
										table.put("fk_user",map.get("fk_user"));
										table.put("week",map.get("week"));
										table.put("sqlMapId", "course_table.insert");
										openDao.insert(table);
									}
								}
							}
						/*}else {
							return output("9999","星期"+result.get(0).get("day")+"的"+result.get(0).get("coursetime")+result.get(0).get("classname")+"这节课已经是"+result.get(0).get("username")+"老师的课程了，一个班的同一节不可同时由两个及两个以上教师上课！","");
						}
					}else {
						return output("9999",result.get(0).get("username")+"老师在"+result.get(0).get("unitname")+"校区已有课程，一个教师不可同时上两个及两个以上校区的课！","");
					}*/
				}else{
					map.put("day", table.get("day"));
					map.put("fk_course_time", table.get("fk_course_time"));
					map.put("sqlMapId", "course_table.delete");
					openDao.delete(map);
				}
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
}
